﻿/*********************************************
 创建者：  jimmy
 创建时间：2016-10-31
 功能说明：岗位资料管理
 ********************************************/
using com.nm.ApiServer;
using com.nm.ApiServer.Attributes;
using com.nm.Attributes;
using com.nm.db.Models.Systems;
using com.nm.Helper;
using EntityFramework.Extensions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaseAPI
{
    [MustLogin]
    [Description("岗位模块的增删改查")]
    public class PostHandler: BaseHandler
    {
        [Description("获取岗位信息列表")]
        [ReturnType(typeof(IList<model.PostData>))]
        public ApiResult GetList([ParamDescription("部门ID")]int DeptID)
        {
            var dc = new SysEntities();
            var result = dc.sys_Post.Where(p => p.IsRemove == false && p.DeptID==DeptID)
                .Select(o => new model.ModifyPostData
            {
                PostID = o.PostID,
                PostName = o.PostName,
                DeptID = o.DeptID,
                Memo = o.Memo,
                IsEffective = o.IsEffective,
                IsRemove = o.IsRemove,
                Creator = o.Creator
            }).ToList();
            return Success(result);
        }

        [Description("获取岗位详细")]
        [ReturnType(typeof(model.ModifyPostData))]
        public ApiResult GetDetails([ParamDescription("岗位ID")]int PostID)
        {
            var dc = new SysEntities();
            var query = dc.sys_Post.FirstOrDefault(p => p.PostID == PostID && p.IsRemove == false);
            if (query == null) return Success(new model.ModifyPostData());
            var result = new model.ModifyPostData
            {
                PostID = query.PostID,
                PostName = query.PostName,
                DeptID = query.DeptID,
                Memo = query.Memo,
                IsEffective = query.IsEffective,
                IsRemove = query.IsRemove,
                Creator = query.Creator
            };
            return Success(result);
        }

        [Description("新建岗位资料")]
        [ReturnType(typeof(int),"0－成功")]
        [JsonType]
        public ApiResult Create([ParamDescription("输入参数")]model.CreatePostData param)
        {
            if (param == null) return Error("参数对象为空");
            if (string.IsNullOrWhiteSpace(param.PostName)) return Error("岗位名称不能为空");
            if (param.PostName.Length > 30) return Error("【岗位名称】长度超出限制");
            if (!string.IsNullOrWhiteSpace(param.Memo) && param.Memo.Length > 50) return Error("【备注】长度超出限制");
            var dc = new SysEntities();
            if (dc.sys_Post.Any(t => t.PostName == param.PostName && t.IsRemove == false)) return Error("岗位名称已存在");
            var post = new sys_Post
            {
                PostName = param.PostName,
                DeptID = param.DeptID,
                IsEffective = param.IsEffective,
                Memo = param.Memo,
                Creator = this.UserId.ToString(),
                CreateDate = DateTime.Now,
                IsRemove = false,
            };
            dc.sys_Post.Add(post);
            try
            {
                dc.SaveChanges();
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }
            return Success(0);
        }

        [JsonType]
        [Description("修改岗位资料")]
        [ReturnType(typeof(int), "0－成功")]
        public ApiResult Modify([ParamDescription("输入参数")]model.ModifyPostData param)
        {
            if (param == null) return Error("参数对象为空");
            if (string.IsNullOrWhiteSpace(param.PostName)) return Error("岗位名称不能为空");
            if (param.PostName.Length > 30) return Error("【岗位名称】长度超出限制");
            if(!string.IsNullOrWhiteSpace(param.Memo))
                if (param.Memo.Length > 50) return Error("【说明】长度超出限制");
            if (param.PostID <= 0) return Error("无效的ID");
            var dc = new SysEntities();
            var query = dc.sys_Post.FirstOrDefault(t => t.PostID == param.PostID);
            if (dc.sys_Post.Any(t => t.PostName == param.PostName && t.IsRemove == false && t.PostID !=param.PostID)) return Error("岗位名称已存在");
            if (query == null) return Error("无数据");
            query.IsEffective = param.IsEffective;
            query.Memo = param.Memo;
            query.PostName = param.PostName;
            query.DeptID = param.DeptID;
            try
            {
                dc.SaveChanges();
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }
            return Success(0);
        }

        [Description("删除岗位资料")]
        [ReturnType(typeof(int), "0－成功")]
        public ApiResult Remove([ParamDescription("岗位ID")] int ID)
        {
            var dc = new SysEntities();
            var user = dc.sys_v_userInfo.FirstOrDefault(t => t.PostID == ID && t.IsRemove == false);
            if (user != null) return Error("该岗位存在用户信息，无法删除！");
            var query = dc.sys_Post.FirstOrDefault(t => t.PostID == ID);
            if (query == null) return Error("无数据");
            //逻辑删除
            query.IsRemove = true;
            try
            {
                dc.SaveChanges();
            }
            catch(Exception ex)
            {
                return Error(ex.Message);
            }
            return Success(0);
        }
    }
}
